package com.huiyeji.hui.utils;

import com.huiyeji.hui.exception.ValidationException;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zhaoduo
 */
public class PageUtil {

    private static final String ONLINE_VALUE = "true";

    private static void analyzeBaseParams(PageParam pageParam,Map<String,String> params){
        int pageSize;
        int pageNum;
        try{
            pageSize = Integer.parseInt(params.get("pageSize"));
            pageNum = Integer.parseInt(params.get("pageNum"));
        }catch (NumberFormatException e){
            throw new ValidationException();
        }
        pageParam.setPageNum(pageNum);
        pageParam.setLeftLimit((pageNum -1)*pageSize);
        pageParam.setRightLimit(pageSize);
    }

    /**
     * 分析params,若不通过验证则抛ValidationException
     * @param params params
     * @return KeyPageParam
     */
    public static KeyPageParam analyzeThenBuildKeyPp(Map<String,String> params){
        KeyPageParam pp = new KeyPageParam();
        analyzeBaseParams(pp,params);
        String keyS = params.get("key");
        if (!StringUtils.hasLength(keyS) || !PatternUtil.test(PatternUtil.PATTERN_ID,keyS)){
            throw new ValidationException();
        }
        long key;
        try{
            key= Long.parseLong(keyS);
        }catch (NumberFormatException e){
            throw new ValidationException();
        }
        pp.setKey(key);
        return pp;
    }

    /**
     * 分析params,若不通过验证则抛ValidationException
     * @param params params
     * @return UserConditionPageParam
     */
    public static UserConditionPageParam analyzeThenBuildUserConditionPp(Map<String,String> params){
        UserConditionPageParam pp = new UserConditionPageParam();
        analyzeBaseParams(pp,params);
        String nickname = params.get("nickname");
        if (!StringUtils.hasLength(nickname) || !PatternUtil.test(PatternUtil.PATTERN_NAME,nickname)){
            throw new ValidationException("昵称输入不合法");
        }
        pp.setNickname(nickname);
        String city = params.get("city");
        if (city != null){
            if ("".equals(city)){
                throw new ValidationException("城市输入不合法");
            }else {
                pp.setCity(city);
                String province = params.get("province");
                pp.setProvince(province == null?"":province);
            }
        }
        String minAgeS = params.get("minAge");
        String maxAgeS = params.get("maxAge");
        if (maxAgeS != null || minAgeS != null){
            int minAge = 0;
            int maxAge = 80;
            try{
                minAge = Integer.parseInt(minAgeS);
                maxAge = Integer.parseInt(maxAgeS);
            }catch (NumberFormatException e){
                throw new ValidationException("年龄范围输入不合法");
            }
            if (maxAge < minAge){
                throw new ValidationException("年龄范围输入不合法");
            }
            pp.setMinAge(minAge);
            pp.setMaxAge(maxAge);
        }
        String online = params.get("online");
        if (online != null){
            if (ONLINE_VALUE.equals(online)){
                pp.setOnline(true);
            } else {
                throw new ValidationException("在线状态输入不合法");
            }
        }
        return pp;
    }

    /**
     * 分析params,若不通过验证则抛ValidationException
     * @param params params
     * @return GroupConditionPageParam
     */
    public static GroupConditionPageParam analyzeThenBuildGroupConditionPp(Map<String,String> params){
        GroupConditionPageParam pp = new GroupConditionPageParam();
        analyzeBaseParams(pp,params);
        String name = params.get("name");
        if (!StringUtils.hasLength(name) || !PatternUtil.test(PatternUtil.PATTERN_NAME,name)){
            throw new ValidationException("群名称输入不合法");
        }
        pp.setName(name);
        return pp;
    }

    private abstract static class PageParam{
        private Integer pageNum;
        private Integer leftLimit;
        private Integer rightLimit;

        public Integer getPageNum() {
            return pageNum;
        }

        public void setPageNum(Integer pageNum) {
            this.pageNum = pageNum;
        }

        public Integer getLeftLimit() {
            return leftLimit;
        }

        public void setLeftLimit(Integer leftLimit) {
            this.leftLimit = leftLimit;
        }

        public Integer getRightLimit() {
            return rightLimit;
        }

        public void setRightLimit(Integer rightLimit) {
            this.rightLimit = rightLimit;
        }
    }

    public static class KeyPageParam extends PageParam{
        private Long key;

        public Long getKey() {
            return key;
        }

        public void setKey(Long key) {
            this.key = key;
        }
    }

    public static class UserConditionPageParam extends PageParam{
        private String nickname;
        private String province;
        private String city;
        private Integer minAge;
        private Integer maxAge;
        private Boolean online;

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public Integer getMinAge() {
            return minAge;
        }

        public void setMinAge(Integer minAge) {
            this.minAge = minAge;
        }

        public Integer getMaxAge() {
            return maxAge;
        }

        public void setMaxAge(Integer maxAge) {
            this.maxAge = maxAge;
        }

        public Boolean getOnline() {
            return online;
        }

        public void setOnline(Boolean online) {
            this.online = online;
        }
    }

    public static class GroupConditionPageParam extends PageParam{
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
